package com.cqc.jdk8.Jutil.stream.collectors;

import com.cqc.eb.UserEB;
import org.junit.Before;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CollectorsTest {

    List<UserEB> userEBList = null;
    Map<String, UserEB> userMap = null;

    @Before
    public void before() {
        userEBList = getUserEBList();
        userMap = getUserMap();
    }


    /**
     * Collectors.toSet() > stream()转Set
     */
    @Test
    public void toSet() {
        Stream<UserEB> stream = userEBList.stream();
        Set<UserEB> userEBSet = stream.collect(Collectors.toSet());
        for (UserEB userEB : userEBSet) {
            System.out.println(userEB.toString());
        }
    }

    /**
     * Collectors.toCollection(...)  >  stream()转ArrayList
     */
    @Test
    public void toCollection() {
        Stream<UserEB> stream = userEBList.stream();
        ArrayList<UserEB> arrayList = stream.collect(Collectors.toCollection(ArrayList::new));
        //也就是
        //ArrayList<UserEB> arrayList = stream.collect(Collectors.toCollection(new Supplier<ArrayList<UserEB>>() {
        //    @Override
        //    public ArrayList<UserEB> get() {
        //        return new ArrayList<UserEB>();
        //    }
        //}));
        System.out.println(arrayList.size());
    }

    /**
     * Collectors.toMap(...)  >  stream()转Map
     * (k1, k2) -> k2  如果key相同，保留k2，也就是保留最后一个
     */
    @Test
    public void toMap() {
//        Map<String, UserEB> map = userEBList.stream()
//                .collect(Collectors.toMap(UserEB::getName, user -> user, (k1, k2) -> k2));
        Map<String, UserEB> map = userEBList.stream()
                .collect(Collectors.toConcurrentMap(UserEB::getName, user -> user, (k1, k2) -> k2));
        System.out.println(map);
        //结果数据
//        李四=UserEB{name='李四', age=21},
//        张三=UserEB{name='张三', age=20},
//        王五=UserEB{name='王五', age=22},
//        刘亦菲=UserEB{name='刘亦菲', age=31},
//        古力娜扎=UserEB{name='古力娜扎', age=26},
//        刘诗诗=UserEB{name='刘诗诗', age=30
//原数据
//        userEBList.add(new UserEB("张三", 20));
//        userEBList.add(new UserEB("李四", 21));
//        userEBList.add(new UserEB("王五", 22));
//        userEBList.add(new UserEB("刘诗诗", 30));
//        userEBList.add(new UserEB("刘亦菲", 31));
//        userEBList.add(new UserEB("古力娜扎", 28));
//        userEBList.add(new UserEB("古力娜扎", 26));
    }

    /**
     * Collectors.toList() > Map的value转成List
     */
    @Test
    public void mapValueToList() {
        //List<UserEB> list = new ArrayList<>(userMap.values());
        List<UserEB> list = userMap.entrySet().stream().map(Map.Entry::getValue).collect(Collectors.toList());
        System.out.println(list);
    }

    /**
     * Array转成String
     */
    @Test
    public void joining() {
        String[] arr = {"a", "b", "c", "d", "e", "f"};
        //a,b,c,d,e,f
//        String a = Arrays.stream(arr).collect(Collectors.joining(","));
        String a = String.join(",", arr);
        //a,b,c,d,e,f
        System.out.println(a);


        String collect = Arrays.stream("a".split(";"))
                .map(x -> x + "A")
                .collect(Collectors.joining(";"));
        System.out.println(collect);//aA
    }


    private List<UserEB> getUserEBList() {
        userEBList = new ArrayList<>();
        userEBList.add(new UserEB("张三", 20));
        userEBList.add(new UserEB("李四", 21));
        userEBList.add(new UserEB("王五", 22));
        userEBList.add(new UserEB("刘诗诗", 30));
        userEBList.add(new UserEB("刘亦菲", 31));
        userEBList.add(new UserEB("古力娜扎", 28));
        userEBList.add(new UserEB("古力娜扎", 26));
        return userEBList;
    }

    private Map<String, UserEB> getUserMap() {
        userMap = new HashMap<>(16);
        userMap.put("1", new UserEB("张三", 20));
        userMap.put("2", new UserEB("李四", 21));
        userMap.put("3", new UserEB("王五", 22));
        return userMap;
    }
}
